Repository navigation
B040: count logger.exception() and similar calls as using the exception - #632
Open
nafiurahman00 wants to merge 1 commit into
Open
nafiurahman00 wants to merge 1 commit into
nafiurahman00 wants to merge 1 commit into
Conversation
B040 fired when a note was added to the caught exception and the exception was then logged with logger.exception(), exc_info=True or traceback.print_exc(). Those all report the exception being handled, note included, so nothing is lost. Passing the exception in (e.g. print(e)) already counted as using it, these calls just don't take it as an argument.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
B040 reports a false positive when a note is added to the caught exception and the exception is then logged without being passed in explicitly:
logger.exception()logs the exception currently being handled, and the note shows up in the output, so nothing was dropped. Same forlogger.error(..., exc_info=True),traceback.print_exc(),traceback.format_exc()andsys.exc_info(). Passingeto any call (evenprint(e)) already counts as using it, these calls just don't take it as an argument.This change treats those calls as a use as well:
.exception(...)call (logging.exception,logger.exception,sys.exception)traceback.print_exc()/traceback.format_exc()/sys.exc_info(), including when imported directly (from traceback import print_exc)exc_info=Trueexc_info=Falseis still reported. I found this by running bugbear on the CPython stdlib, which flags_pyrepl/console.py. That one calls its ownshowsyntaxerror(), which reads the exception fromsys.exc_info()internally, so it's still reported with this change. I didn't try to cover helper functions like that.Added cases to
tests/eval_files/b040.py. They fail without the change, and the test suite and pre-commit pass.